
package tichu_project252.Controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import tichu_project252.Model.Model;
import tichu_project252.View.GameFrame;

/**
 * Handles the New Game button.
 *
 * <p> <b> Invariants: </b> </p>
 *
 * @author Alexandra Barka, ΑΜ: 2867, login : barka.
 * @author Euaggelia Petauraki AM: 2875 login: petavraki.
 * @version %I%, %G%
 *
 */
public class newGameHandler extends Controller implements ActionListener  {
     /**
     * The newGameHandler defines the behavior of the "New Game" buttons when an
     * action is performed on them.
     *
     * <p><b>Constructor: </b> newGameHandler(Model m)</p>
     *
     *<p><b>Precondition: </b> There is a model </p> <p><b>Postcondition:
     * </b> constructs a valid controller </p>
     *
     * @see <a
     * href="http://docs.oracle.com/javase/tutorial/java/IandI/super.html">super</a>
     *
     */
    public newGameHandler(Model m){
        super(m);
    }
    
     /**
     * Invoked when an action occurs.
     * 
     * <p><b>Precondition: </b> The button is pushed </p> 
     * <p><b>Postcondition: </b> The method handleNewGame is performed. </p>
     * 
     *@see ActionListener
     */
    
    @Override
    public void actionPerformed(ActionEvent e) {
        JButton temp = (JButton) e.getSource();
        GameFrame g = (GameFrame) temp.getParent().getParent().getParent().getParent().getParent(); // find the gameframe
        try {
            m.handleNewGame();
            g.cleanup(); // clean the previous game and start new one.
        } catch (Exception ex) {
            Logger.getLogger(newGameHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
}
